package com.cssiot.weixin.basic.kit;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

import com.cssiot.weixin.basic.data.WeixinFinalValue;

/**
 * 文本、图片、event消息处理
 * @author jacking
 *
 */
public class MessageKit {
	private static Map<String,String> replyMsgs = new HashMap<String,String>();
	static{
		replyMsgs.put("123", "你输入了123");
		replyMsgs.put("hello", "world");
		replyMsgs.put("run", "祝你一路平安!");
	}
	
	/**
	 * 接收请求的消息数据转为map
	 * @param req
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,String> reqMsg2Map(HttpServletRequest req) throws IOException {
		String xml = req2xml(req);
		System.out.println("微信平台获取到的数据xml:"+xml);
		try {
			Map<String,String> maps = new HashMap<String, String>();
			Document document = DocumentHelper.parseText(xml);
			Element root = document.getRootElement();
			List<Element> eles = root.elements();
			for(Element e:eles) {
				maps.put(e.getName(), e.getTextTrim());
			}
			return maps;
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取请求的信息数据
	 * @param req
	 * @return
	 * @throws IOException
	 */
	public static String req2xml(HttpServletRequest req) throws IOException {
		BufferedReader br = null;
		br = new BufferedReader(new InputStreamReader(req.getInputStream()));
		String str = null;
		StringBuffer sb = new StringBuffer();
		while((str=br.readLine())!=null) {
			sb.append(str);
		}
		return sb.toString();
	}
	/**
	 * 判断处理类型回复相应类型的消息
	 * @param msgMap
	 * @return
	 * @throws IOException
	 */
	public static String handlerMsg(Map<String, String> msgMap) throws IOException {
		String msgType = msgMap.get("MsgType");
		if(msgType.equals(WeixinFinalValue.MSG_EVENT_TYPE)) {
			return msgType+":"+msgMap.toString();
		} else if(msgType.equals(WeixinFinalValue.MSG_TEXT_TYPE)) {
			return textTypeHandler(msgMap);
		} else if(msgType.equals(WeixinFinalValue.MSG_IMAGE_TYPE)) {
			return imageTypeHandler(msgMap,"_I53ClKoGvcQC4z1mWLf-O_nDJ_rw2p-LtfJOslSONSzUEtv8eKEvlDbn8m71d9m");
		}else{
			return msgType+":"+msgMap.toString();
		}
	}
	/**
	 * 回复图片消息
	 * @param msgMap
	 * @param mediaId
	 * @return
	 * @throws IOException
	 */
	private static String imageTypeHandler(Map<String, String> msgMap,String mediaId) throws IOException {
		Map<String,String> map = new HashMap<String, String>();
		map.put("ToUserName", msgMap.get("FromUserName"));
		map.put("FromUserName", msgMap.get("ToUserName"));
		map.put("CreateTime", new Date().getTime()+"");
		map.put("MsgType", "image");
		map.put("Image", "<MediaId>"+mediaId+"</MediaId>");
		return map2xml(map);
	}
	/**
	 *回复文本 消息
	 * @param msgMap
	 * @return
	 * @throws IOException
	 */
	private static String textTypeHandler(Map<String, String> msgMap) throws IOException {
		Map<String,String> map = new HashMap<String, String>();
		map.put("ToUserName", msgMap.get("FromUserName"));
		map.put("FromUserName", msgMap.get("ToUserName"));
		map.put("CreateTime", new Date().getTime()+"");
		map.put("MsgType", "text");
		String replyContent = "你请求的消息的内容不正确!";
		String con = msgMap.get("Content");
		if(replyMsgs.containsKey(con)) {
			replyContent = replyMsgs.get(con);
		}
		map.put("Content", replyContent);
		return map2xml(map);
	}
	/**
	 *用于回复     map转xml
	 * @param map
	 * @return
	 * @throws IOException
	 */
	public static String map2xml(Map<String, String> map) throws IOException {
		Document d = DocumentHelper.createDocument();
		Element root = d.addElement("xml");
		Set<String> keys = map.keySet();
		for(String key:keys) {
			root.addElement(key).addText(map.get(key));
		}
		StringWriter sw = new StringWriter();
		XMLWriter xw = new XMLWriter(sw);
		xw.setEscapeText(false);
		xw.write(d);
		return sw.toString();
	}
}
